/*
 * Copyright Aristool AG Switzerland
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package org.opprua.tidy.gens;

import java.util.ArrayList;
import java.util.List;

import org.opprua.tidy.util.CodePrint;
import org.opprua.tidy.util.Primitives;
import org.opprua.tidy.util.StringUtil;

/**
 * TODO: add Java doc
 * 
 * Prepared for first commit: 15.04.2007
 * 
 * @author Lin Li
 */
public class GVar extends Gtor {

  private String modifiers;
  private String type;
  private String name;
  private String initializer;

  private String exType;
  private String exInit;
  private boolean isArray;

  private List<String> getterAnnotation = new ArrayList<String>();
  private List<String> setterAnnotation = new ArrayList<String>();

  /**
   * <p>
   * Constructor for the generator of a variable declaration.
   * <p>
   * 
   * @param modifiers
   *          like <tt>static final</tt> can be null or empty.
   * @param type
   *          type of the variable.
   * @param name
   *          name of the variable.
   * @param initializer
   *          initial value, can be null or empty
   */
  public GVar(String modifiers, String type, String name, String initializer) {
    this.modifiers = modifiers;
    this.type = type;
    this.name = name;
    this.initializer = initializer;
    this.isArray = false;
    this.exType = type;
    this.exInit = initializer;
  }

  /**
   * @see Gtor#dump(CodePrint w)
   */
  public void dump(CodePrint w) {

    javadoc(w);
    printAnnotation(w);

    w.print(test(modifiers) ? modifiers + " " : "");
    w.print(type);
    w.printPadded(name);
    w.printPadded(test(initializer) ? "= " + initializer : "");
    w.println(";");
  }

  public String getInitializer() {
    return initializer;
  }

  public String getModifiers() {
    return modifiers;
  }

  public String getName() {
    return name;
  }

  public String getCName() {
    return StringUtil.capFirst(name);
  }

  public void setArrayType() {
    if (Primitives.getFor(type) == null) {
      this.isArray = true;
      this.type = "java.util.List<" + this.exType + ">";
      this.initializer = "java.util.Collections.synchronizedList"
                  + "(new java.util.ArrayList<" + this.getExternalType()
                  + ">())";
    } else {
      this.type += "[]";
      this.exType = this.type;
    }
  }

  public boolean isArray() {
    return isArray;
  }

  public String getExternalType() {
    return exType;
  }

  public String getExInit() {
    return exInit;
  }

  public String getType() {
    return type;
  }

  public void annoteGetter(String s) {
    getterAnnotation.add(s);
  }

  public void annoteSetter(String s) {
    setterAnnotation.add(s);
  }

  public List<String> getGetterAnnotation() {
    return getterAnnotation;
  }

  public List<String> getSetterAnnotation() {
    return setterAnnotation;
  }
}
